From 94fc983db23b57d8d80de17ad48e3860ea691d84 Mon Sep 17 00:00:00 2001 From: robertl Date: Wed, 7 Apr 2004 15:12:54 +0000 Subject: [PATCH] Wrap calls to time(2) so we can freeze time for test suite. --- Makefile | 8 ++++---- cetus.c | 2 +- copilot.c | 2 +- defs.h | 1 + gcdb.c | 2 +- geoniche.c | 2 +- gpilots.c | 2 +- gpsdrive.c | 2 +- gpspilot.c | 2 +- gpsutil.c | 2 +- gpx.c | 2 +- magnav.c | 4 ++-- magproto.c | 4 ++-- mapopolis.c | 4 ++-- mapsource.c | 6 +++--- nmea.c | 4 ++-- pcx.c | 2 +- psitrex.c | 4 ++-- quovadis.c | 2 +- tiger.c | 2 +- util.c | 17 ++++++++++++++++- 21 files changed, 46 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 97976c4ce..e41dbd80c 100644 --- a/Makefile +++ b/Makefile @@ -78,10 +78,10 @@ dep: (echo -n "internal_styles.c: mkstyle.sh " ; echo style/*.style ; /bin/echo -e '\t./mkstyle.sh > $@ || (rm -f $@ ; exit 1)' ) >> /tmp/dep echo Edit Makefile and bring in /tmp/dep -VERSIONU=1_2_2_beta0317a2004 -VERSIOND=1.2.2_beta0317a2004 -VERSIONU=1_2_2 -VERSIOND=1.2.2 +VERSIONU=1_2_3_beta04052004 +VERSIOND=1.2.3_beta04052004 +#VERSIONU=1_2_2 +#VERSIOND=1.2.2 release: cvs commit diff --git a/cetus.c b/cetus.c index 170dd8bf9..fd17a0918 100644 --- a/cetus.c +++ b/cetus.c @@ -378,7 +378,7 @@ data_write(void) } opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; - opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->ctime = opdb->mtime = current_time() + 2082844800U; opdb->type = MYTYPE; /* CWpt */ opdb->creator = MYCREATOR; /* cGPS */ opdb->version = 1; diff --git a/copilot.c b/copilot.c index 44a55e0cb..10d028da4 100644 --- a/copilot.c +++ b/copilot.c @@ -180,7 +180,7 @@ data_write(void) strncpy(opdb->name, out_fname, PDB_DBNAMELEN); opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; - opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->ctime = opdb->mtime = current_time() + 2082844800U; opdb->type = MYTYPE; opdb->creator = MYCREATOR; opdb->version = 0; diff --git a/defs.h b/defs.h index bed556242..4c2317723 100644 --- a/defs.h +++ b/defs.h @@ -410,6 +410,7 @@ int case_ignore_strcmp(const char *s1, const char *s2); char *strsub(char *s, char *search, char *replace); void rtrim(char *s); signed int get_tz_offset(void); +time_t current_time(void); signed int month_lookup(const char *m); const char *get_cache_icon(const waypoint *waypointp); char * xml_entitize(const char * str); diff --git a/gcdb.c b/gcdb.c index b4bd3ac52..84e5725ce 100644 --- a/gcdb.c +++ b/gcdb.c @@ -318,7 +318,7 @@ data_write(void) strncpy(opdb->name, "GeocachingDB", PDB_DBNAMELEN); opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; - opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->ctime = opdb->mtime = current_time() + 2082844800U; opdb->type = MYTYPE; /* CWpt */ opdb->creator = MYCREATOR; /* cGPS */ opdb->version = 1; diff --git a/geoniche.c b/geoniche.c index 12aac56bb..4302ff279 100644 --- a/geoniche.c +++ b/geoniche.c @@ -509,7 +509,7 @@ data_write(void) PdbOut->name[PDB_DBNAMELEN-1] = 0; PdbOut->attributes = PDB_ATTR_BACKUP; - PdbOut->ctime = PdbOut->mtime = time(NULL) + (49*365 + 17*366) * (60*60*24); + PdbOut->ctime = PdbOut->mtime = current_time() + (49*365 + 17*366) * (60*60*24); PdbOut->type = MYTYPE; PdbOut->creator = MYCREATOR; PdbOut->version = 0; diff --git a/gpilots.c b/gpilots.c index b615fde99..fb8f6f1a7 100644 --- a/gpilots.c +++ b/gpilots.c @@ -291,7 +291,7 @@ data_write(void) */ opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; - opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->ctime = opdb->mtime = current_time() + 2082844800U; opdb->type = MYWPT; opdb->creator = MYCREATOR; diff --git a/gpsdrive.c b/gpsdrive.c index 51bd73d15..621fa4976 100644 --- a/gpsdrive.c +++ b/gpsdrive.c @@ -76,7 +76,7 @@ data_read(void) * Make sure that all waypoints in single read have same * timestamp. */ - time_t now = time(NULL); + time_t now = current_time(); do { linecount++; diff --git a/gpspilot.c b/gpspilot.c index 36f4ec071..5edc21513 100644 --- a/gpspilot.c +++ b/gpspilot.c @@ -234,7 +234,7 @@ data_write(void) } opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; - opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->ctime = opdb->mtime = current_time() + 2082844800U; opdb->type = MYTYPE_POINTS; opdb->creator = MYCREATOR; opdb->version = 0; diff --git a/gpsutil.c b/gpsutil.c index c8d5ae85a..2ab25528c 100644 --- a/gpsutil.c +++ b/gpsutil.c @@ -53,7 +53,7 @@ data_read(void) * Make sure that all waypoints in single read have same * timestamp. */ - time_t now = time(NULL); + time_t now = current_time(); for(;fgets(ibuf, sizeof(ibuf), file_in);) { diff --git a/gpx.c b/gpx.c index ff111654b..95ea894bc 100644 --- a/gpx.c +++ b/gpx.c @@ -1197,7 +1197,7 @@ gpx_write(void) int short_length; bounds bounds; - time( &now ); + now = current_time(); if (snlen) short_length = atoi(snlen); diff --git a/magnav.c b/magnav.c index e84837437..392974e40 100644 --- a/magnav.c +++ b/magnav.c @@ -155,7 +155,7 @@ my_writewpt(const waypoint *wpt) tm = gmtime( &wpt->creation_time); } if ( !tm ) { - time( &tm_t ); + tm_t = current_time(); tm = gmtime( &tm_t ); } @@ -248,7 +248,7 @@ data_write(void) strncpy(opdb->name, "Companion Waypoints", PDB_DBNAMELEN); opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; - opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->ctime = opdb->mtime = current_time() + 2082844800U; opdb->type = MYTYPE; /* CWpt */ opdb->creator = MYCREATOR; /* cGPS */ opdb->version = 1; diff --git a/magproto.c b/magproto.c index e84705621..b0f66d8a1 100644 --- a/magproto.c +++ b/magproto.c @@ -720,7 +720,7 @@ mag_rd_init(const char *portname) if (!noack) mag_handon(); - now = time(NULL); + now = current_time(); /* * The 315 can take up to 4.25 seconds to respond to initialization * commands. Time out on the side of caution. @@ -733,7 +733,7 @@ mag_rd_init(const char *portname) while (!got_version) { mag_readmsg(); - if (time(NULL) > later) { + if (current_time() > later) { fatal(MYNAME ": No acknowledgment from GPS on %s\n", portname); } diff --git a/mapopolis.c b/mapopolis.c index ebc8bd83f..fecb23f87 100644 --- a/mapopolis.c +++ b/mapopolis.c @@ -200,7 +200,7 @@ my_writewpt(const waypoint *wpt) tm = gmtime( &wpt->creation_time); } if ( !tm ) { - time( &tm_t ); + tm_t = current_time(); tm = gmtime( &tm_t ); } @@ -296,7 +296,7 @@ data_write(void) strncpy(opdb->name, "Companion Waypoints", PDB_DBNAMELEN); opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; - opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->ctime = opdb->mtime = current_time() + 2082844800U; opdb->type = MYTYPE; /* CWpt */ opdb->creator = MYCREATOR; /* cGPS */ opdb->version = 1; diff --git a/mapsource.c b/mapsource.c index eab8b5e91..a22647c27 100644 --- a/mapsource.c +++ b/mapsource.c @@ -251,7 +251,7 @@ mps_wr_init(const char *fname) } else { fclose(mps_file_out); - srand((unsigned) time(NULL)); + srand((unsigned) current_time()); for (;;) { /* create a temporary name based on a random char and the existing name */ @@ -1143,7 +1143,7 @@ mps_routehdr_w(FILE *mps_file, int mps_ver, const route_head *rte) } if (uniqueValue == 0) { - uniqueValue = time(NULL); + uniqueValue = current_time(); } /* route name */ @@ -1562,7 +1562,7 @@ mps_trackhdr_w(FILE *mps_file, int mps_ver, const route_head *trk) } if (uniqueValue == 0) { - uniqueValue = time(NULL); + uniqueValue = current_time(); } /* track name */ diff --git a/nmea.c b/nmea.c index 2fc81221e..34c39da3e 100644 --- a/nmea.c +++ b/nmea.c @@ -152,7 +152,7 @@ gpgll_parse(char *ibuf) hms = hms / 100; tm.tm_hour = hms % 100; - waypt->creation_time = mktime(&tm) + get_tz_offset() + time(0); + waypt->creation_time = mktime(&tm) + get_tz_offset() + current_time(); if (latdir == 'S') latdeg = -latdeg; waypt->latitude = ddmm2degrees(latdeg); @@ -197,7 +197,7 @@ gpgga_parse(char *ibuf) hms = hms / 100; tm.tm_hour = (long) hms % 100; - waypt->creation_time = mktime(&tm) + get_tz_offset() + time(NULL); + waypt->creation_time = mktime(&tm) + get_tz_offset() + current_time(); if (latdir == 'S') latdeg = -latdeg; waypt->latitude = ddmm2degrees(latdeg); diff --git a/pcx.c b/pcx.c index 07b4d9566..cba08c71d 100644 --- a/pcx.c +++ b/pcx.c @@ -146,7 +146,7 @@ gpsutil_disp(const waypoint *wpt) lat = degrees2ddmm(wpt->latitude); if (tm == 0) - tm = time(NULL); + tm = current_time(); strftime(tbuf, sizeof(tbuf), "%d-%b-%y %I:%M:%S", localtime(&tm)); while (*tp) { *tp = toupper(*tp); diff --git a/psitrex.c b/psitrex.c index fe55b6c5e..f23aea611 100755 --- a/psitrex.c +++ b/psitrex.c @@ -483,7 +483,7 @@ psit_routehdr_w(FILE *psit_file, const route_head *rte) } if (uniqueValue == 0) { - uniqueValue = time(NULL); + uniqueValue = current_time(); } /* route name */ @@ -638,7 +638,7 @@ psit_trackhdr_w(FILE *psit_file, const route_head *trk) } if (uniqueValue == 0) { - uniqueValue = time(NULL); + uniqueValue = current_time(); } /* track name */ diff --git a/quovadis.c b/quovadis.c index bf8796d3d..7300f5971 100644 --- a/quovadis.c +++ b/quovadis.c @@ -238,7 +238,7 @@ data_write(void) } opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; - opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->ctime = opdb->mtime = current_time() + 2082844800U; opdb->type = MYTYPE; /* CWpt */ opdb->creator = MYCREATOR; /* cGPS */ opdb->version = 1; diff --git a/tiger.c b/tiger.c index 312fa0e7d..a850588e3 100644 --- a/tiger.c +++ b/tiger.c @@ -155,7 +155,7 @@ tiger_disp(const waypoint *wpt) pin = wpt->icon_descr ? wpt->icon_descr : ""; else if (wpt->icon_descr && strstr(wpt->icon_descr, "-unfound")) pin = unfoundmarker; - else if (wpt->creation_time > time(0) - 3600 * 24 * thresh_days) + else if (wpt->creation_time > current_time() - 3600 * 24 * thresh_days) pin = newmarker; else pin = oldmarker; diff --git a/util.c b/util.c index e16b53821..8e1967441 100644 --- a/util.c +++ b/util.c @@ -447,7 +447,7 @@ si_round( double d ) signed int get_tz_offset(void) { - time_t now = time(0); + time_t now = current_time(); time_t later = mktime(gmtime(&now)); if (later == -1) { @@ -457,6 +457,21 @@ get_tz_offset(void) } } +/* + * A wrapper for time(2) that allows us to "freeze" time for testing. + */ +time_t +current_time(void) +{ + static char *frozen; + + if (getenv("GPSBABEL_FREEZE_TIME")) { + return 0; + } + + return time(NULL); +} + /* * Return the (zero based) month number of the year or -1 for failure. */ -- 2.30.2